package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.DiscountProductVouchersDAO;
import com.ecoolex.discount.sales.core.service.QuanMaoService;
import com.ecoolex.discount.sales.entity.DiscountOrder;
import com.ecoolex.discount.sales.entity.DiscountProduct;
import com.ecoolex.discount.sales.entity.DiscountProductVouchers;
import com.ecoolex.discount.sales.entity.DiscountProductVouchersExample;
import com.ecoolex.discount.sales.enums.DiscountProductServiceType;
import com.ecoolex.discount.sales.pojo.EditDiscountProductVouchersDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;

import static com.ecoolex.discount.sales.entity.DiscountProductVouchers.Column;
import static com.ecoolex.discount.sales.entity.DiscountProductVouchers.NOT_DELETED;

@Service
public class DiscountProductVouchersManager {
    @Autowired
    private DiscountProductVouchersDAO dao;

    @Autowired
    private QuanMaoService quanMaoService;
    @Autowired
    private DiscountProductInventoryManager discountProductInventoryManager;
    @Autowired
    private DiscountProductManager discountProductManager;

    void validityVerify(DiscountProduct product) {
        // 验证服务是否能支持
        if (DiscountProductServiceType.QUAN_MAO.equal(product.getServiceType())) {
            quanMaoService.validityVerify(product);
        } else if (DiscountProductServiceType.INVENTORY.equal(product.getServiceType())) {
            discountProductInventoryManager.validityVerify(product);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "抵扣券折扣类型不支持此服务类型");
        }
    }

    void onlineVerify(DiscountProduct product) {
        BizAssert.isTrue(Check.notNull(product), "折扣产品不存在");
        DiscountProductVouchers vouchers = getByProductId(product.getProductId());
        BizAssert.isTrue(Check.notNull(vouchers), "折扣抵扣券产品不存在");
        // 验证服务是否能支持
        if (DiscountProductServiceType.QUAN_MAO.equal(product.getServiceType())) {
            quanMaoService.onlineVerify(product, vouchers);
        } else if (DiscountProductServiceType.INVENTORY.equal(product.getServiceType())) {
            discountProductInventoryManager.onlineVerify(product, vouchers);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "卡/券折扣类型不支持此服务类型");
        }
    }

    void create(DiscountProduct product, String createUser) {
        DiscountProductVouchers record = new DiscountProductVouchers();
        record.setProductId(product.getProductId());
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    @Transactional
    public void update(Integer productId, EditDiscountProductVouchersDTO edit, String updateUser) {
        DiscountProductVouchers record = new DiscountProductVouchers();
        record.setCurrencyType(edit.getCurrencyType());
        record.setCounterPrice(edit.getCounterPrice());
        record.setRetailPrice(edit.getRetailPrice());
        record.setMaxBuyNumber(edit.getMaxBuyNumber());
        record.setExpirydateStrategy(edit.getExpirydateStrategy());
        record.setExpirydateDescribe(edit.getExpirydateDescribe());
        record.setFixedStartExpirydate(edit.getFixedStartExpirydate());
        record.setFixedEndExpirydate(edit.getFixedEndExpirydate());
        record.setDynamicExpirydateType(edit.getDynamicExpirydateType());
        record.setDynamicExpirydateValue(edit.getDynamicExpirydateValue());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DiscountProductVouchersExample example = new DiscountProductVouchersExample();
        example.createCriteria()
                .andProductIdEqualTo(productId)
                .andDeletedEqualTo(NOT_DELETED);

        int row = dao.updateByExampleSelective(record, example,
                Column.currencyType,
                Column.counterPrice,
                Column.retailPrice,
                Column.maxBuyNumber,
                Column.expirydateStrategy,
                Column.expirydateDescribe,
                Column.fixedStartExpirydate,
                Column.fixedEndExpirydate,
                Column.dynamicExpirydateType,
                Column.dynamicExpirydateValue,
                Column.updateUser,
                Column.updateTime);

        BizAssert.isTrue(row == 1, "更新抵用券信息失败");

        // 如果上线则需要做参数检查
        discountProductManager.checkIfOnlineAndVerify(productId);
    }

    public DiscountProductVouchers getByProductId(Integer productId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(productId, NOT_DELETED);
    }

    boolean freeze(DiscountOrder order, String updateUser) {
        if (DiscountProductServiceType.INVENTORY.equal(order.getServiceType())) {
            return discountProductInventoryManager.freeze(order, updateUser);
        }
        return true;
    }

    void unfreeze(DiscountOrder order, String updateUser) {
        if (DiscountProductServiceType.INVENTORY.equal(order.getServiceType())) {
            discountProductInventoryManager.unfreeze(order, updateUser);
        }
    }

    public void processOrder(DiscountOrder order, String updateUser) {
        DiscountProductVouchers vouchers = getByProductId(order.getProductId());
        BizAssert.isTrue(Check.notNull(vouchers), "ProductId={}, 卡券配置不存在", order.getProductId());

        if (DiscountProductServiceType.QUAN_MAO.equal(order.getServiceType())) {
            quanMaoService.processOrder(order, vouchers, updateUser);
        } else if (DiscountProductServiceType.INVENTORY.equal(order.getServiceType())) {
            discountProductInventoryManager.processOrder(order, vouchers, updateUser);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "服务类型不支持");
        }
    }
}